package util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


/**
 * <p>
 * Title: 工具类
 * </p>
 * <p>
 * Description: 用来处理时间日期字符串等
 * </p>
 * <p>
 * Copyright: (C) 2006.11 常用方法，需要者随便拿去^-^
 * </p>
 * 
 * @author weiking
 * @version 1.０
 */
public class TimeUtilTool {

	public TimeUtilTool() {
		super();
	}

	/**
	 * 取当前打印日期
	 * 
	 * @return
	 * @throws java.lang.Exception
	 */
	public String getPrintDate() {
		String printDate = "";
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(new Date());
		printDate += calendar.get(Calendar.YEAR) + "年";
		printDate += (calendar.get(Calendar.MONTH) + 1) + "月";
		printDate += calendar.get(Calendar.DATE) + "日";
		return printDate;
	}

	/**
	 * 将指定的日期字符串转化为日期对象
	 * 
	 * @param dateStr
	 *            日期字符串
	 * @return java.util.Date
	 */
	public static Date getDate(String dateStr, String format) throws Exception {

		if (dateStr == null || format == null) {
			throw new Exception("数据类型异常" + dateStr + "|" + format);
		}

		SimpleDateFormat df = new SimpleDateFormat(format);

		try {
			Date date = df.parse(dateStr);
			return date;
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 将指定日期转换为 Timestamp
	 * 
	 * @param date
	 *            指定日期格式为 "20030908"
	 * @return Timestamp
	 * @throws Exception
	 */
	public static Timestamp getTimestamp(String dateStr) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00.000");
		return Timestamp.valueOf(sdf.format(getDate(dateStr, "yyyyMMdd")));
	}

	/**
	 * 从指定Timestamp中得到相应的日期
	 * 
	 * @param datetime
	 *            指定的Timestamp
	 * @return 日期 "2003-09-08"
	 */
	public String getDateFromDateTime(Timestamp datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(datetime).toString();
	}

	/**
	 * 得到当前时间的时间戳
	 * 
	 * @return 当前时间戳
	 */
	public Timestamp getNowTimestamp() {
		long curTime = System.currentTimeMillis();
		return new Timestamp(curTime);
	}

	
	 public static void main(String[] args) throws Exception {  
		//当前时间  
		    Calendar cal  = Calendar.getInstance();  
		    //SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
		    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss G E D F w W a E F");  
		    String mDateTime=formatter.format(cal.getTime());  
		    System.out.println(mDateTime);  
		//1年前日期  
		    java.util.Date myDate=new java.util.Date();   
		    long myTime=(myDate.getTime()/1000)-60*60*24*365;  
		    myDate.setTime(myTime*1000);  
		    String mDate=formatter.format(myDate);  
		    System.out.println(mDate);  
		//明天日期  
		    myDate=new java.util.Date();  
		    myTime=(myDate.getTime()/1000)+60*60*24;  
		    myDate.setTime(myTime*1000);  
		    mDate=formatter.format(myDate);  
		    System.out.println(mDate);  
		//两个时间之间的天数  
		    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
		    java.util.Date date= myFormatter.parse("1883-10-10");  
		    java.util.Date mydate= myFormatter.parse("1899-12-30");  
		    long  day=(date.getTime()-mydate.getTime())/(24*60*60*1000);  
		    System.out.println(day);  
		    
		//加半小时  
		    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
		    java.util.Date date1 = format.parse("2002-02-28 23:16:00");  
		    long Time=(date1.getTime()/1000)+60*30;  
		    date1.setTime(Time*1000);  
		    String mydate1=formatter.format(date1);  
		    System.out.println(mydate1);  
		    System.out.println("秒数"+date1.getTime()); 
		//年月周求日期  
		   SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM F E");  
		   java.util.Date date2= formatter2.parse("2003-05 5 星期五");  
		   SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd");  
		   String mydate2=formatter3.format(date2);  
		   System.out.println(mydate2);  
		//求是星期几  
		   mydate= myFormatter.parse("2007-11-9");  
		   SimpleDateFormat formatter4 = new SimpleDateFormat("E");  
		   String mydate3=formatter4.format(mydate);  
		   System.out.println(mydate3);  
		 }  
}